import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const $time 				= $Lazy.file(()=>import('@com/utils/time'))
const ResetData 			= $Lazy.state(()=>import('@views/_public/reset-data'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '区域',			name: 'region_id', 					type: 'select', loading:true },
			{ label: '项目名称',			name: 'kind_id', 				 	type: 'select', loading:true },
			{ label: '状态',			name: 'status',						type: 'select', loading:true, idStr:'code' },
			{ label: '接收标本时间',		names: ['start_at','end_at'], 		name:'jz-time', type:'date-range', format:1  },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.props.onRef?.(this)
		this.getSelect()
		// 设置时间默认值
		$time.then(f=>{
			const arr = f.duration['1-today']()
			this.resetModel = { start_at: arr[0], end_at: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			if(window.isInit){
				this.fetch()
			}else{
				this.props?.onKeyChange?.()
			}
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 区域
			$fn.setRow(forms, 'region_id', await f.getCache({ api:'area_adm' } ) ) 
			// 项目名称
			$fn.setRow(forms, 'kind_id', await f.getCache({ api:'project_select' } ) ) 
			// 状态
			$fn.setRow(forms, 'status', await f.getDicCache({ code:45300 } ) )
			// 账单状态
			const bill_status = await f.bill_status
			
			this.setState({ forms, bill_status })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'st-bill-region/businessIndex', {isIndex:true, callback: result => this.setState({ result })})
	
	// 表单
	cols = [
		{ title: '序号',				dataIndex: 'index', 					width:50,	render: text => text + 1 },
		{ title: '区域名称',				dataIndex: 'region_name', 				width:200 },
		{ title: '项目名称',				dataIndex: 'kind_name', 				width:200 },
		{ title: '状态',				dataIndex: 'status', 					width:100,	render: text => $fn.getState(this.state.bill_status, text) },
		{ title: '数量',				dataIndex: 'sum_bill', 					...$fn.table.num },
		{ title: '冲抵数量',				dataIndex: 'sum_bill_out', 				...$fn.table.num },
		{ title: '实际数量',				dataIndex: 'sum_bill_in', 				...$fn.table.num },
		{ title: '标准价格',				dataIndex: 'sum_sprice', 				...$fn.table.num },
		{ title: '实际价格',				dataIndex: 'sum_price', 				...$fn.table.num },
		{ title: '成本',				dataIndex: 'cost_price', 				...$fn.table.num }
	]

	// 操作按钮
	controls = () => {
		return [
			Controls.exports.call(this, 'st-bill-region/businessIndex'),						// 导出
			// 重置数据
			Controls.click.call(this, '生成/重置数据', () => {
				this.resetDataRef.open({type:1,api:'st-bill-region/reset'})
			})
		]
	}

	render(){
		const { result } = this.state
		return (
			<>
				{/* 搜索 */}
				{ SearchForm.call(this, { timeStr:'1-today' }) }
				{/* 表格 */}
				<div className='fv ex'>
					{ Table.call(this,{type:2, isIndex:true, isInit: window.isInit,
						suffix: (
							<>
								<div className='ml10 g9'>总数量 <b className='f14 c0'>{result?.total_sum_bill}</b></div>
								<div className='ml10 g9'>总冲抵数量 <b className='f14 c0'>{result?.total_sum_bill_out}</b></div>
								<div className='ml10 g9'>总实际数量 <b className='f14 c0'>{result?.total_sum_bill_in}</b></div>
							</>
						)
					}) }
				</div>
                {/* 重置数据 */}
				<ResetData onRef={ref => this.resetDataRef = ref} />
			</>
		)
	}
}

export default Index